package com.dhms.vostok1.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.dhms.home.R
import com.dhms.home.databinding.ToDoCenterListItemBinding

import com.dhms.vostok1.data.todo.ToDoCenterData


class ToDoCenterListAdapter(private val onItemClick: (item: ToDoCenterData?) -> Unit) :
    PagingDataAdapter<ToDoCenterData, ToDoCenterListAdapter.ToDoCenterListViewHolder>(
        ToDoListComparator
    ) {

    object ToDoListComparator : DiffUtil.ItemCallback<ToDoCenterData>() {
        override fun areItemsTheSame(
            oldItem: ToDoCenterData,
            newItem: ToDoCenterData
        ): Boolean {
            // Id is unique.
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: ToDoCenterData,
            newItem: ToDoCenterData
        ): Boolean {
            return oldItem == newItem
        }
    }

    class ToDoCenterListViewHolder(val binding: ToDoCenterListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ToDoCenterData?) {
            binding.item = item

            binding.typeImageView.apply {
                setImageDrawable(
                    ContextCompat.getDrawable(
                        context,
                        if (item?.isTask == true) R.drawable.ic_to_do_center_list_task else R.drawable.ic_to_do_center_list_approval
                    )
                )
            }
            item?.values?.forEachIndexed { i, t ->
                when (i) {
                    0 -> {
                        binding.numberOneNameTextView.apply {
                            text = t.name ?: context.getString(R.string.null_string)
                        }
                        binding.numberOneValueTextView.apply {
                            text = t.value ?: context.getString(R.string.null_string)
                        }
                    }
                    1 -> {
                        binding.numberTwoNameTextView.apply {
                            text = t.name ?: context.getString(R.string.null_string)
                        }
                        binding.numberTwoValueTextView.apply {
                            text = t.value ?: context.getString(R.string.null_string)
                        }
                    }
                    else -> {
                        binding.numberThreeNameTextView.apply {
                            text = t.name ?: context.getString(R.string.null_string)
                        }
                        binding.numberThreeValueTextView.apply {
                            text = t.value ?: context.getString(R.string.null_string)
                        }
                    }
                }
            }


            binding.executePendingBindings()
        }


    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ToDoCenterListViewHolder {
        val binding =
            ToDoCenterListItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        return ToDoCenterListViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ToDoCenterListViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)

        holder.binding.cardWrapper.setOnClickListener {
            onItemClick(item)
        }
    }
}